"""
文本预处理和大模型总结构成列链
1.读取文本数据
2.文本预处理函数  1). 按自然段拆分文本 2). 取前 N 段 3). 合并为单一文本 4). 其他处理如替换
3.构建总结提示词模板
4.将预处理数据函数包装以个 RunnableLambda对象
5.构建序列链RunnableSequence对象，    RunnableLambda对象，总结提示词模板，llm,StrOutputParser
6.执行链条  传入文本数据
"""
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.output_parsers import StrOutputParser
from langchain_core.runnables import RunnableSequence
from model_utils import getLLM
from langchain_core.runnables import RunnableSequence,RunnableLambda

with open("/root/project/Code/sshcode/lc/character.txt","r",encoding="utf-8") as f:
    file_content = f.read()

def preprocess(inputs:str,paragraphs_num:int=3):
    text = inputs.split("\n\n")
    text = text[:paragraphs_num]
    text = "\n\n".join(text)
    text = text.replace("（虚构）","")
    return text

template = ChatPromptTemplate.from_messages([
    ("system","你是一个文本总结助手"),
    ("human","总结文本{text}")
])

#包装预处理文本对象
text_run = RunnableLambda(lambda x:{"text":preprocess(x['inputs'])})

chain = RunnableSequence(text_run,template,getLLM())

r = chain.invoke({"inputs":file_content})
print(r)